Data loading

# use the API to pull data
organ <- GET("https://health.data.ny.gov/resource/km5a-7zrs.csv?$limit=10000") %>% 
  content("parsed") %>%
  janitor::clean_names() %>%
  filter(county != "TOTAL NYS" & county != "Out of State" & county != "Unknown") %>%
  mutate(year = as.character(year)) %>%
  mutate(month = as.character(month)) %>%
  mutate(dummy_day = as.character("01")) %>%
  mutate(date = (str_c(year, month, dummy_day, sep = "-"))) %>%
  mutate(date = as.Date(date, "%Y-%m-%d"))
## Parsed with column specification:
## cols(
##   `_2012_census_population` = col_integer(),
##   chart_month = col_character(),
##   county = col_character(),
##   eligible_population_enrolled = col_double(),
##   location = col_character(),
##   month = col_integer(),
##   opo = col_character(),
##   population_18_estimate = col_integer(),
##   registry_enrollments = col_integer(),
##   year = col_integer()
## )
# Tidy the data for plot the donation map

organ_tidy = 
  organ %>% 
  separate(location, c("lat", "long"), sep = ",") %>% 
  mutate(long = str_replace(long, "\\)", ""),
         long = as.numeric(long)) %>% 
  mutate(lat = str_replace(lat, "\\(", ""),
         lat = as.numeric(lat)) 

Mapping

ny = map_data("state") %>% 
  filter(region == "new york")

ny_county = map_data("county")  %>% 
  filter(region == "new york") %>% 
  as.tibble() %>% 
  rename(county = subregion)

organ_df = 
  organ_tidy %>% 
  select(eligible_population_enrolled, county) %>% 
  mutate(county = tolower(county)) %>% 
  mutate(county = recode(county,  'cattauragus' = 'cattaraugus'))

ny_county_combined = 
full_join(organ_df, ny_county, by = 'county')

df_1 = organ_df %>% distinct(county) 
df_2 = ny_county %>% distinct(county) 
p = 
ggplot() + 
  geom_polygon(data = ny_county_combined, 
               aes(x = long, 
                   y = lat, 
                   group = group, 
                   fill = eligible_population_enrolled)) +
  geom_path(data = ny_county_combined, 
            aes(x = long, 
                y = lat,
                group = group), 
            color = "white", 
            size = 0.1) +
  geom_text(data = organ_tidy, 
            aes(x = long, 
                y = lat, 
                label = county),
            size = 2.5,
            color = "white") +
  labs(x = 'Longitude', y = 'Latitude', title = 'map', fill = '% Enrollment') +
  coord_equal() +
  viridis::scale_fill_viridis(option = "magma", direction = -1)

ggplotly(p)
# Trying to use library leaflet to make it interactive
leaflet(ny) %>% 
  addPolylines(lng = ny$long, lat = ny$lat, group = ny$order) %>% 
  addPolygons(lng = ny$long, lat = ny$lat, group = ny$order, color = "#444444", weight = 1, smoothFactor = 0.5,
    opacity = 1.0, fillOpacity = 0.5,
    
    highlightOptions = highlightOptions(color = "white", weight = 2,
      bringToFront = TRUE))

Plot the registry proportion as points reflecting by size.

ggplot() + 
  geom_polygon(data = ny, aes(x = long, y = lat, group = group), fill = "darkgray") + 
  geom_polygon(data = ny_county, aes(x = long, y = lat, group = group), fill = NA, color = "white") +
  geom_point(data = organ_tidy, aes(x = long, y = lat, size = eligible_population_enrolled, color = eligible_population_enrolled)) +
  theme_void()

Plot the registry proportion with heat map

ny_state_map = 
ggplot() + 
  geom_polygon(data = ny, aes(x = long, y = lat, group = group), fill = "darkgrey") + 
  geom_polygon(data = ny_county, aes(x = long, y = lat, group = group), fill = NA, color = "white") +
  stat_density2d(data = organ_tidy, aes(x = long, y = lat, fill = ..level.., alpha = 0.25), size = 0.2, bins = 10, geom = "polygon") +
  geom_density2d(data = organ_tidy, aes(x = long, y = lat), size = 0.3) +
  coord_fixed(1.4) +
  theme_bw() +
  scale_fill_gradient(low = "light blue", high = "dark blue") 
ny_state_map